<?php  
## include required files
/*******************************/
require_once '../model/blogs.php';
/*******************************/
## Create Objects
/*******************************/
$blogObj = new Model_Blogs();
/*******************************/

if(isset($_POST['addblog_btn'])) {
	## apply PHP validation for required filed
	if(trim($_POST['title']) != '' && trim($_POST['description']) != '') {
		//********************************************UPlOADING PHOTO*************************************************************//
		if($_FILES['blog_image']['name'] != '') {
			
				$size =	getimagesize($_FILES['blog_image']['tmp_name']);
				
					if($_FILES['blog_image']['name'] != '' && ($size[0] >= BLOGIMAGEWIDTH && $size[1] >= BLOGIMAGEHEIGHT) ) {
						
						## Upload image
						$path = '../uploads/blog/';
						$imageName    = $_FILES['blog_image']['name'];
						$arrImageName = explode(".",$imageName);
						$position     = count($arrImageName);
						$fileName     = date('Ymdhis').".".$arrImageName[$position-1];
						copy($_FILES["blog_image"]["tmp_name"], $path.$fileName);
					
						$blogArray = array();
						extract($_POST);
						
						$blogArray['title'] = return_post_value($_POST['title']);
						$blogArray['introtext'] = return_post_value($introtext);
						$blogArray['description'] = return_post_value($description);
						$blogArray['status'] = '1';
						$blogArray['user_id'] = $_SESSION["admin_id"];
						$blogArray['added_date'] = date('Y-m-d');
						$blogArray['image'] = $fileName;
						
						$blog_id = $blogObj->addblog($blogArray);
						
						$_SESSION['msg'] = "<div class='success_msg'><span>Blog added successfully</span></div>";
			    	    header('location:'.SITE_URL.'/admin/home.php?q=blogs');
			        	exit;
				} else {
						$_SESSION['msg'] = "<div class='error_msg'><span>".BLOGIMAGE_WIDTHHEIGHT."</span></div>";
				}	
			} else {
				$_SESSION['msg'] = "<div class='error_msg'><span>Please upload blog image</span></div>";	
			}
       //************************************************************************************************************
		
	} else {// php validation else
		$_SESSION['msg'] = "<div class='error_msg'><span>Please enter blog title and description</span></div>";
	}
	// main if closed
}

## Assign success or error msg to smarty variable and unset session variable
if(trim($_SESSION['msg']) != '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}

## Set active class variable for left menu
$smarty->assign('activeclass', 'blogs');
$smarty->assign('mainmenu', '3');
## Unset all the objects created which are on this page
unset($blogObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'blogs/addblog.tpl');
unset($smarty);
?>